package com.gdevelop.examples.demo.client;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;

public class BaseController {
  /**
   * Simple email validator
   */
  private class EmailValidator implements IValidator{
    public IStatus validate(Object value) {
      String email = (String)value;
      if ((email != null) && (email.length() > 0)){
        int pos;
        if ((pos = email.indexOf("@")) < 0){
          return ValidationStatus.error("Missing @ in email address");
        }
        if ((pos = email.indexOf(".", pos)) < 0){
          return ValidationStatus.error("Missing '.' in email address");
        }
        if (pos >= email.length()-1){
          return ValidationStatus.error("email should be in .com, .org, etc format");
        }
      }
      return ValidationStatus.ok();
    }
  }

  /**
   * User for demo purpose
   */
  private User user = Demo.users.get(0);

  private EmailValidator emailValidator = new EmailValidator();
  public BaseController() {
  }

  /**
   * @return The constants used during demo
   */
  public DemoConstants getConstants(){
    return Demo.constants;
  }

  /**
   * @return The Email validator
   */
  public IValidator getEmailValidator(){
    return emailValidator;
  }

  /**
   * @return The demo user
   */
  public User getUser(){
    return user;
  }

  /**
   * @return the User list
   */
  public UserList getUserList(){
    return Demo.users;
  }
}
